﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 串口转发.Communication.UDPClient;

namespace 串口转发
{
    

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        private int _TotalBytes;
        public int TotalBytes
        {
            get => _TotalBytes; set
            {
                _TotalBytes = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TotalBytes"));
            }
        }
        private int _TotalUdp;
        public int TotalUdp
        {
            get => _TotalUdp; set
            {
                _TotalUdp = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("TotalUdp"));
            }
        }

        private ComPortAttr PortAttr;
        
        private UartServer uartServer;

        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindow()
        {
            InitializeComponent();

            InitSerialSet();

            DataContext = this;
        }

        
        public void InitSerialSet()
        {
            InitPorts();
            PortAttr.Baudrate = 5000000;
            PortAttr.Databit = 8;
            PortAttr.Parity = Parity.None;
            PortAttr.Stopbit = StopBits.One;

        }

        public void InitPorts()
        {
            string[] portNames = SerialPort.GetPortNames();
            string lastName = "";

            PortsBox.Items.Clear();
            if (portNames == null)
            {
                return;
            }
            
            foreach (string portName in portNames)
            {
                PortsBox.Items.Add(portName);
                lastName = portName; 
            }

            PortsBox.Text = lastName;
            PortAttr.ComportName = lastName;
        }


        private void ReceiveData(UartStateEventArgs args)
        {
            TotalBytes += Encoding.UTF8.GetByteCount(args.datas);
            //Console.WriteLine(Encoding.UTF8.GetString(args.datas) + $" {TotalBytes}");
            //Console.WriteLine($"{TotalBytes} {TotalUdp}");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            uartServer = new UartServer(PortAttr);
            uartServer.UartReceived += new UartReceivedEventHandler(ReceiveData);
            Console.WriteLine("已打开串口" + PortsBox.Text);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            uartServer?.Stop();
            Console.WriteLine("已关闭串口");
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            uartServer?.UartTransmit(Encoding.UTF8.GetBytes("abcdefg"));
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            string locateIP = "127.0.0.1";
            int locatePort = 5000;
            UDPClient udpClient = new UDPClient(locateIP, locatePort);
            udpClient.UDPMessageReceived += UdpClient_UDPMessageReceived;
            Console.WriteLine("已建立udp连接");
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            TotalBytes = 0;
            TotalUdp = 0;
        }

        public void UdpClient_UDPMessageReceived(UdpStateEventArgs args)
        {
            //var remotePoint = args.remoteEndPoint;
            //string info = Encoding.UTF8.GetString(args.buffer);

            //Console.WriteLine($"from {remotePoint} received: {info}");

            uartServer?.UartTransmit(args.buffer);
            if (args.buffer == null)
                Console.WriteLine("null");
            TotalUdp += args.buffer.Length;
        }
    }

}
